package com.coda.controller.user;

import com.coda.common.Result;
import com.coda.constant.MessageConstant;
import com.coda.context.BaseContext;
import com.coda.pojo.WordSetting;
import com.coda.service.WordSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/wordSetting")
public class WordSettingController {
    @Autowired
    private WordSettingService wordSettingService;

    /**
     * 获取当前用户的单词设置
     */
    @GetMapping
    public Result<WordSetting> getSetting() {
        WordSetting result = wordSettingService.getSetting(BaseContext.getCurrentId());
        return Result.success(result);
    }

    /**
     * 单词默写设置
     */
    @PostMapping("/setting")
    public Result<String> wordSetting(@RequestBody WordSetting wordSetting) {
        //设置用户id
        wordSetting.setUserId(BaseContext.getCurrentId());
        int result = wordSettingService.setWordSetting(wordSetting);
        if (result > 0) {
            return Result.success(MessageConstant.SETTING_SUCCESS);
        }
        return Result.error(MessageConstant.WORD_SETTING_ERR);
    }

    /**
     * 删除单词设置
     */
    @DeleteMapping()
    public Result<String> deleteWordSetting(){
        int result = wordSettingService.deleteWordSetting();
        if (result > 0) {
            return Result.success(MessageConstant.DELETE_SUCCESS);
        }
        return Result.error(MessageConstant.DELETE_ERR);
    }
}
